package com.suke.money.Web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.money.suke.utils.Constants;
import com.money.suke.utils.Result;
import com.suke.money.model.FinanceAccount;
import com.suke.money.model.User;
import com.suke.money.service.FinanceAccountService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: liush
 * @CreateTime: 2022-12-02  09:01
 * @Description: 账户控制层
 */
@Controller
public class FinanceController {

    @Reference(interfaceClass = FinanceAccountService.class, version = "1.0.0", timeout = 20000)
    FinanceAccountService financeAccountService;

    @RequestMapping("/loan/page/finance")
    @ResponseBody
    public Object finance(HttpServletRequest request) {
        //校验：是否登录
        User user = (User) request.getSession().getAttribute(Constants.LOGIN_USER);
        if (!ObjectUtils.allNotNull(user)) {
            return Result.error("请先登录后，再查询余额");
        }
        FinanceAccount financeAccount = financeAccountService.queryFinanceAccountByUserId(user.getId());
        if (!ObjectUtils.allNotNull(financeAccount)) {
            return Result.error("系统升级中，请稍后查询");
        }
        request.getSession().setAttribute("financeAccount", financeAccount);
        return Result.success(financeAccount.getAvailableMoney() + "");
    }
}
